package com.tang.controller;


import com.tang.dto.CategoryDTO;
import com.tang.dto.DocDTO;
import com.tang.resp.JsonResp;
import com.tang.service.impl.CategoryServiceImpl;
import com.tang.service.impl.DocServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/doc/*")
public class DocController {
    @Autowired
    private DocServiceImpl docService;
    @GetMapping("all")
    public JsonResp all(){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        resp.setMessage("查询文档成功");
        List<DocDTO> all = docService.all();
        resp.setData(all);
        return resp;
    }
    @Validated
    @GetMapping("list")
    public JsonResp list(String name, @NotBlank String ebookId, @NotNull int pageNum, @NotNull int pageSize){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        resp.setMessage("查询文档成功");
        Map<String, Object> map = docService.list(name,ebookId,pageNum, pageSize);
        resp.setData(map);
        return resp;
    }
    @GetMapping("listChildren")
    public JsonResp listChildren(String id){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        List<DocDTO> dtos = docService.listChildren(id);
        if (ObjectUtils.isNotEmpty(dtos)){
            resp.setMessage("查询子文档成功");
        }else {
            resp.setMessage("查询子文档失败,当前文档可能无子文档");
        }
        resp.setData(dtos);
        return resp;
    }
    @GetMapping("listByEbookId")
    public JsonResp listByEbookId(String ebookId){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        List<DocDTO> dtos = docService.listByEbookId(ebookId);
        if (ObjectUtils.isNotEmpty(dtos)){
            resp.setMessage("查询文档成功");
        }else {
            resp.setMessage("查询文档失败");
        }
        resp.setData(dtos);
        return resp;
    }
    @GetMapping("oneContent/{id}")
    public JsonResp oneContent(@PathVariable String id){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        String content = docService.oneContent(id);
        if (ObjectUtils.isNotEmpty(content)){
            resp.setMessage("查询文档内容成功");
        }else {
            resp.setMessage("查询文档内容失败");
        }
        resp.setData(content);
        return resp;
    }
    @PostMapping("update")
    public JsonResp update(@RequestBody DocDTO docDTO){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        int update = docService.update(docDTO);
        if (update>0){
            resp.setMessage("更新文档成功");
        }else {
            resp.setMessage("更新文档失败");
        }
        resp.setData(update);
        return resp;
    }
    @PostMapping("updateVote")
    public JsonResp updateVote(String id){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        int update = docService.updateVote(id);
        if (update>0){
            resp.setMessage("点赞成功");
        }else {
            resp.setMessage("点赞失败,点赞过多次");
        }
        resp.setData(update);
        return resp;
    }
    @DeleteMapping("delete/{id}")
    public JsonResp delete(@PathVariable("id") String id){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        int delete = docService.delete(id);
        if (delete>0){
            resp.setMessage("删除文档成功");
        }else {
            resp.setMessage("删除文档失败");
        }
        resp.setData(delete);
        return resp;
    }
    @DeleteMapping("deleteBatch")
    public JsonResp deleteBatch(List<String> ids){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        int delete = docService.deleteBatch(ids);
        if (delete>0){
            resp.setMessage("删除文档成功");
        }else {
            resp.setMessage("删除文档失败");
        }
        resp.setData(delete);
        return resp;
    }
}
